package cn.gson.crm.model.dao;

import java.sql.Date;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

import cn.gson.crm.model.Jdbc;
import cn.gson.crm.model.entity.InterViewVo;
import cn.gson.crm.model.entity.Interview;

public class InterviewDao {
	/**
	 * 新增面试记录
	 * @param inter
	 * @return
	 * @throws SQLException
	 */
	public Long insertInterview(Interview inter) throws SQLException{
		String sql="insert into interview(interview_student_id,interview_company,interview_address,interview_summary)"
				+ " value(?,?,?,?)";
		if(Jdbc.getInstace().executeUpdate(sql,inter.getInterviewStudentId(),inter.getInterviewCompany(),inter.getInterviewAddress(),inter.getInterviewSummary())){
				String sqls = "select interview_id id from interview order by interview_id desc limit 1";
				Map<String, Object> map = Jdbc.getInstace().executeQuery(sqls);
				if (map != null) {
					return Long.parseLong(map.get("id").toString());
				}
		}
		return null;
		}
	/**
	 *查询面试记录 
	 * @throws SQLException 
	 */
	public InterViewVo geteInterviewByname(String name) throws SQLException{
		String sql="select * from interview a inner join student b on a.interview_student_id=b.student_id where b.student_name=?";
		List<Map<String,Object>> list=Jdbc.getInstace().executeQueryForList(sql, name);
		InterViewVo listview=null;
		if(list!=null&&list.size()>0){
			listview=new InterViewVo();
			for (Map<String,Object> m : list) {
				listview=buid(m);
			}
		}
		return listview;
	}
	/**
	 *查询附件
	 * @throws SQLException 
	 */
	public List<InterViewVo> geteInterviewById(String id) throws SQLException{
		String sql="select * from file a inner join interview b on a.recod=b.interview_id"
				+ " where recod in(select interview_id from interview where interview_student_id=?)";
		List<Map<String,Object>> list=Jdbc.getInstace().executeQueryForList(sql, id);
		List<InterViewVo> listview=null;
		if(list!=null&&list.size()>0){
			listview=new ArrayList<InterViewVo>();
			for (Map<String,Object> m : list) {
				listview.add(buid(m));
			}
		}
		return listview;
	}
	public InterViewVo buid(Map<String,Object> map){
		InterViewVo  view=new InterViewVo();
		if (map.get("student_name") != null) {
			view.setStudentName(map.get("student_name").toString());
		}
		if (map.get("interview_id") != null) {
			view.setInterviewId(Integer.parseInt(map.get("interview_id").toString()));
		}
		if (map.get("interview_company") != null) {
			view.setInterviewCompany(map.get("interview_company").toString());
		}
		if (map.get("interview_address") != null) {
			view.setInterviewAddress(map.get("interview_address").toString());
		}
		if (map.get("interview_time") != null) {
			SimpleDateFormat sf=new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss");
			try {
				java.util.Date  d=sf.parse(map.get("interview_time").toString());
				Date  date=new Date(d.getTime());
				view.setInterviewTime(date);
			} catch (ParseException e) {
				e.printStackTrace();
			}
	}
		if (map.get("interview_summary") != null) {
			view.setInterviewSummary(map.get("interview_summary").toString());
		}
		if (map.get("interview_recording") != null) {
			view.setInterviewRecording(map.get("interview_recording").toString());
		}
		if (map.get("interview_topic") != null) {
			view.setInterviewTopic(map.get("interview_topic").toString());
		}
		if (map.get("file_name") != null) {
			view.setFileName(map.get("file_name").toString());
		}
		if (map.get("file_path") != null) {
			view.setFilePath(map.get("file_path").toString());
		}
		if (map.get("model") != null) {
			view.setModel(map.get("model").toString());
		}
		if (map.get("recod") != null) {
			view.setRecod(Long.parseLong(map.get("recod").toString()));
		}
		if (map.get("file_size") != null) {
			view.setFileSize(Double.parseDouble(map.get("file_size").toString()));
		}
		
		return view; 
	}
}
 